package com.metis.document.parse.dialog.utils;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.IOException;

public class ScanPDFDetector {

    private final static Integer DEFAULT_CONTENT_SIZE = 50;

    public static Boolean isPdf(ByteArrayMultipartFile file) {
        PDDocument document = null;
        try {
            document = PDDocument.load(file.getInputStream());

            PDFTextStripper pdfStripper = new PDFTextStripper();
            String text = pdfStripper.getText(document);
            text = text.replaceAll("\\s","");
            text = text.replaceAll("\\n","");
            text = text.trim();

            return text.length() > DEFAULT_CONTENT_SIZE;
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (document != null) {
                try {
                    document.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }
}